/**
 * @file XListViewHeader.java
 * @create Apr 18, 2012 5:22:27 PM
 * @author Maxwin
 * @description XListView's header
 */
package com.jy.app.lib.view.uilistview;


import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.jy.app.lib.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class UIListViewHeader extends LinearLayout {

    private LinearLayout mContainer;

    private int mState = STATE_NORMAL;

    private PercentHeaderView mPercentHeaderView;

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_REFRESHING = 2;

    private String lastFreshDateTimeStr = dateToString(new Date(), "MEDIUM");


    public UIListViewHeader(Context context) {
        super(context);
        initView(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public UIListViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutParams lp = new LayoutParams(
                LayoutParams.FILL_PARENT, 0);
        mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.w_listview_pull_refresh_header_temp, null);
        addView(mContainer, lp);
        setGravity(Gravity.BOTTOM);
        mPercentHeaderView = (PercentHeaderView) findViewById(R.id.header_view);
        mPercentHeaderView.recovery();
    }

    public void setState(int state) {
        if (state == mState) return;

        switch (state) {
            case STATE_NORMAL:
                if (mState == STATE_READY) {

                }
                if (mState == STATE_REFRESHING) {

                }
                mPercentHeaderView.setText(getResources().getString(R.string.xlistview_header_hint_normal));
//			mHeaderTimeView.setText(lastFreshDateTimeStr);
                break;
            case STATE_READY:
                if (mState != STATE_READY) {
                    mPercentHeaderView.setText(getResources().getString(R.string.xlistview_header_hint_ready));
//				mHeaderTimeView.setText(lastFreshDateTimeStr);
                }
                break;
            case STATE_REFRESHING:
                mPercentHeaderView.setText(getResources().getString(R.string.xlistview_header_hint_loading));
                mPercentHeaderView.setRotate(true);
                lastFreshDateTimeStr = dateToString(new Date(), "MEDIUM");
                break;
            default:
        }

        mState = state;
    }

    public void setComplete(){
        mPercentHeaderView.setText("加载完成");
        mPercentHeaderView.setComplete(true);
    }

    public void setError(){
        mPercentHeaderView.setText("加载失败");
        mPercentHeaderView.setComplete(false);
    }

    public void reset(){
//        LogUtil.i("---------reset-------");
        mPercentHeaderView.recovery();
    }

    public void setVisiableHeight(int height, int progress) {
        if (height < 0)
            height = 0;
        LayoutParams lp = (LayoutParams) mContainer
                .getLayoutParams();
        lp.height = height;
        mContainer.setLayoutParams(lp);
        mPercentHeaderView.setProgress(progress);
    }

    public void setVisiableHeight(int height) {
        if (height < 0)
            height = 0;
        LayoutParams lp = (LayoutParams) mContainer
                .getLayoutParams();
        lp.height = height;
        mContainer.setLayoutParams(lp);
        if(height==0){
            reset();
        }
    }

    public int getVisiableHeight() {
        return mContainer.getHeight();
    }

    public static String dateToString(Date date, String type) {
        String str = null;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (type.equals("SHORT")) {
            // 07-1-18
            format = DateFormat.getDateInstance(DateFormat.SHORT);
            str = format.format(date);
        } else if (type.equals("MEDIUM")) {
            // 2007-1-18
            format = DateFormat.getDateInstance(DateFormat.MEDIUM);
            str = format.format(date);
        } else if (type.equals("FULL")) {
            // 2007年1月18日 星期四
            format = DateFormat.getDateInstance(DateFormat.FULL);
            str = format.format(date);
        }
        return str;
    }

}
